package com.suncp.common;

import lombok.Data;
import org.slf4j.MDC;

import java.io.Serializable;

/**
 * Notice:
 *
 * @author scp
 * @version 1.0
 * @date 2021/3/21
 * @since 1.0
 */
@Data
public class GeneralResponse implements Serializable {
    private static final long serialVersionUID = 5810975679211734777L;

    /**
     * 请求流水号
     */
    private final String txnSerialId = MDC.get("RequestID");

    /**
     * 响应时间戳
     */
    private long timestamp = System.currentTimeMillis();

    /**
     * 响应码:{@link RespCodes}
     */
    private String code;

    /**
     * 响应描述:{@link RespCodes}
     */
    private String msg;

    /**
     * 响应业务数据集合
     */
    private Object data;

    /**
     * 创建响应成功实体对象
     * @param data 响应主体数据
     * @return 响应实例
     */
    public static GeneralResponse success(Object data){
        GeneralResponse success = new GeneralResponse();
        success.setData(data);
        success.setCode(RespCodes.TX0000.getCode());
        success.setMsg(RespCodes.TX0000.getText());
        return success;
    }

    /**
     * 创建响应成功实体对象，没有响应数据
     * @return 响应对象
     */
    public static GeneralResponse success(){
        return success(null);
    }

    /**
     * 创建响应失败的实体对象
     * @param code 响应码
     * @param msg 响应信息
     * @return 响应实体
     */
    public static GeneralResponse fail(String code,String msg){
        GeneralResponse fail = new GeneralResponse();
        fail.setCode(code);
        fail.setMsg(msg);
        return fail;
    }

    /**
     * 通过响应枚举对象创建响应失败的实体
     * @param code 响应枚举对象
     * @return 响应实体
     */
    public static GeneralResponse fail(RespCodes code){
        GeneralResponse fail = new GeneralResponse();
        fail.setCode(code.getCode());
        fail.setMsg(code.getText());
        return fail;
    }

    /**
     * 创建响应失败的实体对象
     * @param code 响应码
     * @return 响应实体
     */
    public static GeneralResponse fail(String code){
        return fail(code,RespCodes.explain(code));
    }

    /**
     * 创建响应失败的实体对象
     * @return 响应实体
     */
    public static GeneralResponse fail(){
        return fail(RespCodes.TX9999.getCode(),RespCodes.TX9999.getText());
    }
}
